<template>
    <div class="register-wrap">
        <div class="ms-register">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="param" :rules="rules" ref="register" label-width="0px" class="ms-content">
                <div class="ms-title" style="color:black">注册</div>
                <el-form-item prop="adminName">
                    <el-input v-model="param.adminName" placeholder="adminName">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="adminPassword">
                    <el-input
                        type="password"
                        placeholder="adminPassword"
                        v-model="param.adminPassword"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="adminPasswordQ">
                    <el-input
                        type="password"
                        placeholder="adminPasswordQ"
                        v-model="param.adminPasswordQ"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="phone">
                    <el-input v-model="param.phone" placeholder="phone">
                        <el-button slot="prepend" icon="el-icon-phone"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="Email">
                    <el-input v-model="param.Email" placeholder="Email">
                        <el-button slot="prepend" icon="el-icon-eleme"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="name">
                    <el-input v-model="param.name" placeholder="name">
                        <el-button slot="prepend" icon="el-icon-user"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="role">
                    <el-radio v-model="param.role" label="1" :checked="role=='1'?true:false"  role="1" >门店管理员</el-radio>
                    <el-radio v-model="param.role" label="2" :checked="role=='2'?true:false"  role="2">供应商管理员</el-radio>
                </el-form-item>
                <div class="register-btn">
                    <el-button type="primary" @click="submitForm()">注册</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import { loginAdmin,isAdmin, addAdmin } from '../../services/admin.js';
export default {
    data: function() {
        var validatePass =async (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入账号'));
            } else {
                if (!/^\d{11}$/.test(value)) {
                  callback(new Error("账号格式不正确"));
                } else {
                //   console.log(this.param.adminName,"this.param.adminName");
                  let date = await loginAdmin({
                        adminName: this.param.adminName,
                    });
                  if (date) {
                    callback(new Error("账号重复"));
                  } else {
                    callback();
                  }
                }
            }
        };
        var validatePassPassword =async (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (!/^\d{6,}$/.test(value)) {
                  callback(new Error("密码格式不正确"));
                } else {
                    callback();
                }
            }
        };
        var validatePassPasswordQ =async (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请确认密码'));
            } else {
                if (!/^\d{6,}$/.test(value)) {
                  callback(new Error("密码格式不正确"));
                }  else if (value !== this.param.adminPassword) {
                  callback(new Error("两次输入密码不一致!"));
                } else {
                  callback();
                }
            }
        };
        var validatePassPhone =async (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入电话号码'));
            } else {
                if (!/^\d{11}$/.test(value)) {
                  callback(new Error("电话号码格式不正确"));
                } else {
                  console.log(this.param.phone,"this.param.phone");
                  let date = await isAdmin({
                        phone: this.param.phone,
                    });
                    console.log(date,"date");
                  if (date) {
                    callback(new Error("电话号码重复"));
                  } else {
                    callback();
                  }
                }
            }
        };
        return {
            param: {
                // adminName: '12345678910',
                // adminPassword: '767969845',
                // adminPasswordQ: '767969845',
                // phone: '15928093291',
                // Email: '767969841@qq.com',
                // name: '蒋雯承1',
                // role: '',
                // state: '0',
                adminName: '',
                adminPassword: '',
                adminPasswordQ: '',
                phone: '',
                Email: '',
                name: '',
                role: '',
                state: '0'
            },
            role: '',
            rules: {
                adminName: [{ required: true,validator: validatePass, trigger: 'blur' }],
                adminPassword: [{ required: true,validator: validatePassPassword,trigger: 'blur' }],
                adminPasswordQ: [{ required: true,validator: validatePassPasswordQ, trigger: 'blur' }],
                phone: [{ required: true,validator: validatePassPhone, trigger: 'blur' }],
                Email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
                name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                role: [{ required: true, message: '请输入角色', trigger: 'blur' }]
            }
        };
    },
    methods: {
        submitForm() {
            this.$refs.register.validate(async valid => {
                if (valid) {
                    let { adminName, adminPassword, adminPasswordQ, phone, Email, name, role, state } = this.param;
                    console.log(adminName, adminPassword, adminPasswordQ, phone, Email, name, role, state);
                    let date = await addAdmin({
                        adminName: this.param.adminName,
                        adminPassword: this.param.adminPassword,
                        phone: this.param.phone,
                        Email: this.param.Email,
                        name: this.param.name,
                        role: this.param.role,
                        state: this.param.state
                    });
                    this.$message.success('注册成功');
                    localStorage.setItem('ms_adminName', this.param.adminName);
                    this.$router.push('/login');
                } else {
                    this.$message.error('信息有误');
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
.register-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-register {
    position: absolute;
    left: 50%;
    top: 30%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.register-btn {
    text-align: center;
}
.register-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.register-tips {
    font-size: 12px;
    line-height: 30px;
    color: black;
}
</style>